<?php
/**
 * Resume  
 * 
 * @package Resume
 
 */
// ******************** REGISTER EVENT HANDLERS ******************
elgg_register_event_handler ( 'pagesetup', 'system', 'resume_pagesetup' );
elgg_register_event_handler ( 'init', 'system', 'resume_init' );
function resume_init() {
	// Set timezone to HoChiMin
	date_default_timezone_set ( "Asia/Ho_Chi_Minh" );
	elgg_register_page_handler ( 'resumes', 'resume_page_handler' );
	// register actions
	elgg_register_action ( "resume/addlanguage", elgg_get_plugins_path () . "resume/actions/resume/addlanguage.php" );
	elgg_register_action ( "resume/addskill", elgg_get_plugins_path () . "resume/actions/resume/addskill.php" );
	elgg_register_action ( "resume/addresearch", elgg_get_plugins_path () . "resume/actions/resume/addresearch.php" );
	elgg_register_action ( "resume/addaward", elgg_get_plugins_path () . "resume/actions/resume/addaward.php" );
	elgg_register_action ( "resume/savepersonal", elgg_get_plugins_path () . "resume/actions/resume/savepersonal.php" );
	elgg_register_action ( "resume/editprojects", elgg_get_plugins_path () . "resume/actions/resume/editprojects.php" );
	
	// elgg_register_plugin_hook_handler ( 'entity:icon:url', 'object', 'award_icon_url_override' );
	
	// Add menu item to logged users
	if (elgg_is_logged_in ()) {
	}
	
	// add resume link to profile menu
	elgg_register_plugin_hook_handler ( 'register', 'menu:owner_block', 'resume_owner_block_menu' );
	
	// Extend CSS with plugin's CSS
	$css_url = 'mod/resume/views/default/css/main.css';
	elgg_register_css ( 'special', $css_url );
	elgg_load_css ( 'special' );
	
	// Extend search
	elgg_register_entity_type ( 'object', 'skill' );
	elgg_register_entity_type ( 'object', 'persional' );
	elgg_register_entity_type ( 'object', 'award' );
}

/**
 * Setup page by user logged
 */
function resume_pagesetup() {
	$page_owner = elgg_get_page_owner_entity ();
	$loggedin_username = elgg_get_logged_in_user_entity ()->username;
	
	// Add submenu items to the page
	if (elgg_get_context () == "resumes") {
		// Add page owner's exclusive items to menu
		if ($page_owner->guid == elgg_get_logged_in_user_entity ()->guid) {
			
			elgg_register_menu_item ( 'page', array (
					'name' => 'skill',
					'text' => elgg_echo ( 'resume:add:skill' ),
					'href' => "resumes/skill",
					'priority' => "3" 
			) );
			
			elgg_register_menu_item ( 'page', array (
					'name' => 'award',
					'text' => elgg_echo ( 'resume:add:award' ),
					'href' => "resumes/awards",
					'priority' => "2" 
			) );
			
			elgg_register_menu_item ( 'page', array (
					'name' => 'editproject',
					'text' => elgg_echo ( 'resume:edit:listproject' ),
					'href' => "resumes/projects/edit",
					'priority' => "1" 
			) );
		}
	} else if (elgg_is_logged_in ()) {
		// Not "Page owner's" exclusive items
		elgg_register_menu_item ( 'userprofile', array (
				'name' => 'goto',
				'text' => elgg_echo ( 'resume:menu:goto' ),
				'href' => "resumes/" . $loggedin_username 
		) );
		if (elgg_is_active_plugin ( 'protovis' )) {
			elgg_register_menu_item ( elgg_echo ( 'resume:chronogram' ), "mod/resume/chronogramme.php?id=" . $page_owner->guid, "main_resume" );
		}
	}
	
	// Add "cancel" option if the user is in a create/edit form
	if (elgg_get_context () == "resumes_form") {
		elgg_register_menu_item ( 'page', array (
				'name' => 'cancel',
				'text' => elgg_echo ( 'resume:menu:cancel' ),
				'href' => "resumes/" . $loggedin_username 
		) );
	}
}

/**
 *
 * @param
 *        	$page
 * @param
 *        	$identifier
 * @return boolean
 */
function resume_page_handler($page, $identifier) {
	$base = elgg_get_plugins_path () . 'resume';
	$page_path = $base . '/pages/resume';
	$action_path = $base . '/actions/resume';
	$vars = array ();
	switch ($page [0]) {
		case 'personal' :
			require "$page_path/personal.php";
			break;
		case 'skill' :
			if (isset ( $page [1] )) {
				$vars ['group_guid'] = $page [1];
			}
			require "$page_path/skill.php";
			break;
		case "editskill" :
			$vars['delete'] = "skill";
			$vars ['entity_guid'] = $page [1];
			include "$page_path/skill.php";
			break;
		case "delskill" :
			action_gatekeeper("delskill");
			$vars ['object_guid'] = $page [1];
			include "$action_path/del.php";
			break;
		case 'awards' :
			require "$page_path/award.php";
			break;
		case "editaward":
			$vars ['entity_guid'] = $page [1];
			include "$page_path/award.php";
			break;
		case "delaward":
			$vars['delete'] = "award";
			action_gatekeeper("delaward");
			$vars ['object_guid'] = $page [1];
			include "$action_path/del.php";
			break;
		case "projects" :
			include "$page_path/editprojects.php";
			break;
		default :
			if (isset ( $page [0] )) {
				$vars ['page'] = $page [0];
			} elseif (elgg_is_logged_in ()) {
				$vars ['page'] = elgg_get_logged_in_user_entity ()->username;
			} else {
				return false;
			}
			require_once "$page_path/index.php";
			break;
	}
	
	return true;
}
function resume_owner_block_menu($hook, $type, $items, $params) {
	// $item = new ElggMenuItem ( 'resume', elgg_echo ( 'resume:menu:item' ), $url );
	// doesn't work as it should?
	// $item->setPriority(1);
	if (elgg_instanceof ( $params ['entity'], 'group' )) {
		$group = $params ['entity'];
		if ($group->grouptype == PROJECT_GROUP && $group->isMember ( elgg_get_logged_in_user_guid () )) {
			
			$url = "resumes/skill/{$group->getGUID()}";
			
			$item = new ElggMenuItem ( 'skillproject', elgg_echo ( "Kĩ năng có được" ), $url );
			$items [] = $item;
		}
	} else if (elgg_instanceof ( $params ['entity'], 'user' )) {
		$owner = elgg_get_page_owner_entity ();
		
		if ($owner instanceof ElggUser) {
			$url = "resumes/{$params['entity']->username}";
			$items ['resume'] = ElggMenuItem::factory ( array (
					'name' => 'resume',
					'text' => elgg_echo ( 'resume:menu:item' ),
					'href' => $url 
			) );
		}
	}
	
	return $items;
}

if (! function_exists ( 'unhtmlentities' )) {
	function unhtmlentities($chaineHtml) {
		$tmp = get_html_translation_table ( HTML_ENTITIES );
		$tmp = array_flip ( $tmp );
		$chaineTmp = strtr ( $chaineHtml, $tmp );
		return $chaineTmp;
	}
}
function award_url_override($entity) {
	$title = $entity->title;
	$title = elgg_get_friendly_title ( $title );
	return "resume/view/" . $entity->getGUID () . "/" . $title;
}
